package model.searchers.linkedListSearcher;

/**
 * Class LinkedList represents a linked list of words, where each element in the list points to the next.
 * Hence the first element of the list is the complete list. 
 */

public class LinkedList implements Cloneable {
	private String value;
	private LinkedList next;

	/**
	 * Constructor of LinkedList. 
	 * Creates a linked list with the specified value for the first element and the specified next element
	 */
	public LinkedList(String value, LinkedList next) {
		this.value = value;
		this.next = next;
	}

	/**
	 * @return the value of the first element of the list
	 */
	public String getValue() {
		return value;
	}

	/**
	 * Set the value of the first element of the list to the specified value
	 * 
	 * @param newValue	a String with the new value
	 */
	public void setValue(String newValue) {
		this.value = newValue;
	}

	/**
	 * @return the next element of the list
	 */
	public LinkedList getNext() {
		return next;
	}

	/**
	 * Set the next element of the list to the specified element
	 * 
	 * @param newNext	element to be set as new next element of list
	 */
	public void setNext(LinkedList newNext) {
		this.next = newNext;
	}
	

	/**
	 * Find the LinkedList object corresponding to a specified value (word), if the word is in the list.
	 * Else it returns null.
	 * 
	 * @param value	word to look for in list
	 * @return LinkedList object with value equal to the specified
	 */
	public LinkedList getFromList(String value) {
		LinkedList current = this;
		while (current != null) {
			if (current.getValue().equals(value)) {
				return current;
			} else {
				current = current.getNext();
			}
		}
		return null;
	}

	/**
	 * Find the n'th LinkedList object of the list, if it exist. Else it returns null.
	 * 
	 * @param n number of element to find
	 * @return n'th element of the list
	 */
	//is used to create samples for benchmarking
	public LinkedList getFromList(int n) {
		LinkedList current = this;
		int counter = 0;
		while (current != null) {
			if (n == counter) {
				return new LinkedList(current.getValue(), null);
			} else {
				current = current.getNext();
			}
			counter += 1;
		}
		return null;
	}

	/**
	 * Create a copy of the list
	 */
	@Override
	public LinkedList clone() {
		LinkedList resultTemp = new LinkedList(this.getValue(), null);
		LinkedList startOfNewCopy = resultTemp;
		LinkedList copyOfNextElement = null;

		LinkedList next = this.getNext();

		while (next != null) {
			copyOfNextElement = new LinkedList(next.getValue(), null);

			resultTemp.setNext(copyOfNextElement);
			resultTemp = resultTemp.getNext();
			next = next.getNext();
		}
		return startOfNewCopy;
	}

}
